package com.zzty.txj1.util;

import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;

public class EmailUtil {
    @Value("${spring.mail.host}")
    private String hostName;
    @Value("${spring.mail.username}")
    private String userName;
    @Value("${spring.mail.password}")
    private String password;
    private Logger logger = LoggerFactory.getLogger(EmailUtil.class);
    public String sendEmail(String newEmail,String emailContent,String userName) {

        String result = "success";
        logger.info("===initialDelay: send email begin ");
        HtmlEmail email = new HtmlEmail();
        email.setHostName(hostName); // 设定smtp服务器
        email.setAuthentication(userName, password); // 设定smtp服务器的认证资料信息
        try {
            email.addTo(newEmail, userName); // 设定收件人
            email.setFrom(userName, "自在天原");
            email.setSubject("打卡周报！");
            email.setCharset("UTF-8");
            email.setHtmlMsg("<html>\n" +
                    "<style type='text/css'>\n" +
                    "        table.gridtable {\n" +
                    "        font-family: verdana,arial,sans-serif;\n" +
                    "        color:#333333;\n" +
                    "        border-width: 1px;\n" +
                    "        border-color: #666666;\n" +
                    "        border-collapse: collapse;\n" +
                    "        }\n"+
                    "        table.gridtable th {\n" +
                    "        border-width: 1px;\n" +
                    "        padding: 8px;\n" +
                    "        border-style: solid;\n" +
                    "        border-color: #666666;\n" +
                    "        background-color: #dedede;\n" +
                    "        }\n" +
                    "        table.gridtable td {\n" +
                    "        border-width: 1px;\n" +
                    "        padding: 8px;\n" +
                    "        border-style: solid;\n" +
                    "        border-color: #666666;\n" +
                    "        background-color: #ffffff;\n" +
                    "        }\n" +
                    "</style>\n" +
                    "<table align='center' border='1' cellspacing='0' cellpadding='3' class='gridtable'>\n" +
                    "<tr>\n" +
                    "<th>姓名</th>\n" +
                    "<th>日期</th>\n" +
                    "<th>科目</th>\n" +
                    "<th>打卡情况</th>\n" +
                    "<th>最终结果</th>\n" +
                    "</tr>\n"+
                    emailContent +
                    "</table>\n" +
                    "</html>");
            email.send();
            logger.info("===initialDelay: success");
        } catch (EmailException e) {
            e.printStackTrace();
            logger.info("===initialDelay: error");
        }
        return result;
    }
}

